using Xml;

namespace Atlantis
{
    public class ProjectReader : GLib.Object
    {
		string fileName;
		Xml.Doc* xmldoc;
		Xml.Node* currentNode;
		Xml.Ns* ns;
		
		public ProjectReader(string fileName) {
			this.fileName = fileName;
		}
		
		public ProjectNode read_project() {
			var project = new ProjectNode();
			xmldoc = Parser.parse_file (fileName);
			currentNode = xmldoc->get_root_element ();
			stderr.printf("\n currentNode =" + currentNode->name);
			parse_solution(project, currentNode);
			return project;
		}
		
		public void parse_solution (ProjectNode project, Xml.Node* node) {
			//loop over the passed node's children
			for (Xml.Node* iter = node->children; iter != null; iter = iter->next) {
				//spaces btw. tags are also nodes, discard them
				if (iter->type != ElementType.ELEMENT_NODE)
					continue;
				stderr.printf("\n currentNode =" + iter->name);
				string node_name = iter->name; //get the node's name
				
				switch(node_name){
					case "project":
						parse_project(project, iter);
						break;
					default:
						stderr.printf("\n error in parse_solution() while reading project -> " + node_name);
						break;
				}
				
				//parse_solution(project, iter);
			}
		}
		
		public void parse_project(ProjectNode project, Xml.Node* node) {
			//loop over the passed node's properties (attributes)
			for (Xml.Attr* prop = node->properties; prop != null; prop = prop->next) {
				string attr_name = prop->name;

				//notice the ->children which points to a Node* (Attr doesn't feature content)
				string attr_content = prop->children->content;
				
				switch(attr_name){
					case "name":
						project.Text = attr_content;
						break;
				}
			}


			
			for (Xml.Node* iter = node->children; iter != null; iter = iter->next) {
				//spaces btw. tags are also nodes, discard them
				if (iter->type != ElementType.ELEMENT_NODE)
					continue;

				stderr.printf("\n currentNode =" + iter->name);
				string node_name = iter->name; //get the node's name
				
				switch(node_name){
					case "document":
						var document = new DocumentNode();
						parse_document(document, iter);
						project.Children.add(document);
						break;
					default:
						stderr.printf("error in parse_project() while reading project -> " + node_name);
						break;
				}
				
				//parse_project(project, iter); 
			}
		}
		
		public void parse_document(DocumentNode document, Xml.Node* node) {
			//loop over the passed node's properties (attributes)
			for (Xml.Attr* prop = node->properties; prop != null; prop = prop->next) {
				string attr_name = prop->name;

				//notice the ->children which points to a Node* (Attr doesn't feature content)
				string attr_content = prop->children->content;
				
				switch(attr_name){
					case "name":
						document.Text = attr_content;
						break;
				}
			}
			
			for (Xml.Node* iter = node->children; iter != null; iter = iter->next) {
				//spaces btw. tags are also nodes, discard them
				if (iter->type != ElementType.ELEMENT_NODE)
					continue;

				string node_name = iter->name; //get the node's name
				
				switch(node_name){
					case "path":
						if (document == null)
							stderr.printf("document == null");
						document.RelativePath = iter->get_content();
						break;
					default:
						stderr.printf("error in parse_document() while reading project -> " + node_name);
						break;
				}
				
				//parse_document(document, iter);	
			}
		}
    }
}
